package makabakaquestionnaire.zutsoftware.controller;

import cn.hutool.core.date.DateUtil;
import io.swagger.annotations.ApiOperation;
import makabakaquestionnaire.zutsoftware.annotation.Log;
import makabakaquestionnaire.zutsoftware.entity.ProjectTemplate;
import makabakaquestionnaire.zutsoftware.entity.ProjectTemplateItem;
import makabakaquestionnaire.zutsoftware.entity.ProjectTheme;
import makabakaquestionnaire.zutsoftware.entity.page.TableDataInfo;
import makabakaquestionnaire.zutsoftware.entity.vo.TemplatePreviewVo;
import makabakaquestionnaire.zutsoftware.enums.BusinessType;
import makabakaquestionnaire.zutsoftware.enums.ProjectStatusEnum;
import makabakaquestionnaire.zutsoftware.framework.security.utils.SecurityUtils;
import makabakaquestionnaire.zutsoftware.service.IProjectTemplateItemService;
import makabakaquestionnaire.zutsoftware.service.IProjectTemplateService;
import makabakaquestionnaire.zutsoftware.service.ProjectThemeService;
import makabakaquestionnaire.zutsoftware.system.common.AjaxResult;
import makabakaquestionnaire.zutsoftware.system.common.BaseController;
import makabakaquestionnaire.zutsoftware.utils.UUidUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 评价模板Controller
 *
 * @author wjf
 * @date 2022-04-03
 */
@RestController
@RequestMapping("/template")
public class ProjectTemplateController extends BaseController {
    @Autowired
    private IProjectTemplateService projectTemplateService;
    @Autowired
    private IProjectTemplateItemService projectTemplateItemService;
    @Autowired
    private ProjectThemeService projectThemeService;

    /**
     * 查询评价模板列表
     */
    @PreAuthorize("@ss.hasPermi('system:template:list')")
    @GetMapping("/list")
    public TableDataInfo list(ProjectTemplate projectTemplate) {
        startPage();
        List<ProjectTemplate> list = projectTemplateService.selectProjectTemplateList(projectTemplate);
        return getDataTable(list);
    }

    /**
     * 获取评价模板详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:template:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(projectTemplateService.selectProjectTemplateById(id));
    }

    /**
     * 新增评价模板
     */
    @PreAuthorize("@ss.hasPermi('system:template:add')")
    @Log(title = "评价模板", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProjectTemplate projectTemplate) {
        projectTemplate.setProjectKey(UUidUtil.getSimpleUUID());
        projectTemplate.setStatus(ProjectStatusEnum.CREATE.getValue());
        String now = DateUtil.now();
        Date date = DateUtil.parse(now);   //转为date格式
        projectTemplate.setCreateTime(date);
        projectTemplate.setUpdateTime(date);
        projectTemplate.setCreateBy(SecurityUtils.getUsername());
        projectTemplateService.insertProjectTemplate(projectTemplate);
        return AjaxResult.success("操作成功",projectTemplate.getProjectKey());
    }

    /**
     * 将已创建的评价问卷生成为模板1
     */
//    @PostMapping("/save")
//    public Result saveAsProjectTemplate(@RequestBody UserProject request, @RequestAttribute Long userId) {
//        UserProject userProject = userProjectService.queryProjectByProKey(request.getProjectKey());
//        List<ProjectItem> itemEntityList = projectItemService.queryProjectItemByProKey(request.getProjectKey());
//        TemplateProject templateProject = new TemplateProject();
//        templateProject.setProjectKey(UUidUtil.getSimpleUUID());
//        templateProject.setCategoryId(4L); //4为其他，后面可以搞个枚举类型来表示
//
//        templateProjectService.insertProject(templateProject);//考虑改为mq情况，包装为消息：key放在message的bod里，另一个系统里，消费者拿着key可得到project和item，分别将他们保存在模板的数据库中
//        itemEntityList.forEach(item -> item.setProjectKey(templateProject.getProjectKey()));
//        templateProjectItemService.save(projectTemplateItemList);
//        return Result.success(templateProject.getProjectKey());
//    }

    /**
     * 修改评价模板
     */
    @PreAuthorize("@ss.hasPermi('system:template:edit')")
    @Log(title = "评价模板", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProjectTemplate projectTemplate) {

        return toAjax(projectTemplateService.updateProjectTemplate(projectTemplate));
    }

    /**
     * 删除评价模板
     */
    @PreAuthorize("@ss.hasPermi('system:template:remove')")
    @Log(title = "评价模板", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(projectTemplateService.deleteProjectTemplateByIds(ids));
    }

    @ApiOperation("预览问卷所需的数据（project,projectItem,projectTheme）")
    @GetMapping("/preview/{proKey}")
    public AjaxResult previewTemplate(@PathVariable String proKey){
        ProjectTemplate userProject = projectTemplateService.selectProjectTemplateByKey(proKey);
        ProjectTemplateItem projectTemplateItem=new ProjectTemplateItem();
        projectTemplateItem.setProjectKey(proKey);
        List<ProjectTemplateItem> projectItems = projectTemplateItemService.selectProjectTemplateItemList(projectTemplateItem);
        ProjectTheme projectTheme = projectThemeService.queryProjectThemeByProKey(proKey);
        TemplatePreviewVo templatePreviewVo = new TemplatePreviewVo(userProject, projectItems, projectTheme);
        return AjaxResult.success("模板预览数据",templatePreviewVo);
    }

}
